package com.xflib.tool.builder;

import cn.hutool.core.util.StrUtil;
import org.slf4j.LoggerFactory;

import java.io.File;

public class MvnBuilder extends AbstractBuilder {
    public MvnBuilder(BuilderFactory builderFactory) {
        super(builderFactory);
        super.setLogger(LoggerFactory.getLogger(this.getClass()));
    }

    @Override
    public String getId() {
        return "mvn";
    }

    @Override
    public String getDescribe() {
        return "maven builder.";
    }

    @Override
    protected String getCommandCheckout(BuilderProjectProperties properties, String branch) {
        return "git checkout ".concat(branch)/*.concat(" ").concat(sourceUrl).concat(" & git.exe reset --hard & git pull")*/;
    }

    @Override
    protected String getCommandClone(BuilderProjectProperties properties) {
        return "git clone ".concat(properties.getSourceUrl());
    }

    @Override
    protected String getCommandPull(BuilderProjectProperties properties) {
        return "git pull";
    }

    @Override
    protected String getCommandBuild(BuilderProjectProperties properties1) {
        //return "cmd /c call mvn -Dmaven.test.skip=true clean package -s ".concat(properties.getGit().getConfigPath());
        String setting = builderProperties.getMvn().getConfigPath().trim();
        if (null==setting || setting.trim().isEmpty() || !(new File(setting.trim()).exists())) {
            setting="";
        } else {
            setting = " -s ".concat(setting);
        }

        String profile=properties1.getProfile();
        if(null==profile || profile.trim().isEmpty()){
            profile = "";
        } else {
            profile=" -P".concat(profile.trim());
        }

        return "mvn -Dmaven.test.skip=true clean package".concat(setting).concat(profile);
    }

    @Override
    protected String getCommandDeploy(BuilderProjectProperties project,String zipFileName){
        String remoteTarget = project.getRemotePath().trim();
        if(remoteTarget.endsWith("/")) {
            remoteTarget = remoteTarget.substring(0, remoteTarget.length()-1);
        }
        String projectName = project.getProjectName();
        if(StrUtil.isBlank(remoteTarget) || remoteTarget.equals("/")){
            return "echo work path can not on `pwd`.;echo '>>>>>>' "+projectName+" install failed.;";
        }

        return "source /etc/profile;"
                + "source ~/.bash_profile;"
                + "if [ ! -d \"/tmp\" ];then mkdir /tmp;fi;cd /tmp;"  // 为了避免在根目录操作出错，设置工作目录为/tmp
                + "ZIP_FILE=/tmp/"+ zipFileName +";"
                + "PRJ_NAME=" + projectName + ";"
                + "PRJ_PATH=" + remoteTarget + ";"
                + "BAK_PATH=${PRJ_PATH}/../../backup;"
                + "if [ ! -d \"${PRJ_PATH}\" ];then mkdir -p ${PRJ_PATH};fi;"
                + "if [ ! -d \"${BAK_PATH}\" ];then mkdir -p ${BAK_PATH};fi;"
                + "if [ ! -f \"${PRJ_PATH}/app.sh\" ];then touch \"${PRJ_PATH}\"/app.sh;chmod +x \"${PRJ_PATH}\"/app.sh;fi;"
                + "pushd `pwd`;cd ${PRJ_PATH};./app.sh stop;popd;"
                + "vTmp0=`ps -ef|grep ${PRJ_NAME}|grep -v grep|awk '{print $2}'|xargs echo`;"
                +  "if [ ! \"X${vTmp0}\" = \"X\" ];then kill ${vTmp0};fi;"
                + "TLT=`stat --format=%y ${PRJ_PATH}/app.sh`;TLT=`date -d \"${TLT}\" +%Y%m%d%H%M%S`;"
                + "if [ ! -d \"${BAK_PATH}\" ];then mkdir ${BAK_PATH};fi;"
                + "mv ${PRJ_PATH} ${BAK_PATH}/${PRJ_NAME}.${TLT};" // 之前的目标备份到backup
                + "unzip -O UTF-8 ${ZIP_FILE} -d ${PRJ_PATH};" // 在java程序中(环境是UTF-8)压缩的, 所以UTF-8，若是windows下压缩的，则用CP936
                + "chmod +x ${PRJ_PATH}/*.sh;"
                + "pushd `pwd`;cd ${PRJ_PATH};./app.sh start debug;popd;"
                + "vTmp1=`ps -ef|grep ${PRJ_NAME}|grep -v grep|awk '{print $2}'|xargs echo`;"
                + "vTmp2=`cat ${PRJ_PATH}/logs/${PRJ_NAME}.pid`;"
                + "if [ \"X${vTmp1}\" = \"X${vTmp2}\" ];"
                + "then echo ${PRJ_NAME} install success.;"
                + "else echo ${PRJ_NAME} install failed.;"
                + "fi";
    }
}
